package com.bjsxt.livegoods.search.service.service.impl;

import com.bjsxt.livegoods.entity.Product;
import com.bjsxt.livegoods.es.dao.ProductDao4ES;
import com.bjsxt.livegoods.es.vo.Product4Search;
import com.bjsxt.livegoods.dao.product.ProductDao;
import com.bjsxt.livegoods.search.service.service.SearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 搜索服务提供者-服务实现
 */
@Service
public class SearchServiceImpl implements SearchService {

    @Autowired
    private ProductDao4ES productDao4ES;

    @Autowired
    private ProductDao productDao;
    @Override
    public Map<String, Object> searchProducts(String city, String content, int page, int rows) {
        Map<String,Object> maps=
                this.productDao4ES.searchProduct(city,content,page,rows);
        return maps;
    }

    //同步数据，查询MongoDB中所有的商品数据，保存到ElasticSearch中
    @Override
    public void sync() {
      //创建索引
        productDao4ES.createIndex();
        //设置映射
        productDao4ES.putMapping();
        //同步数据
        Query query=new Query();
        List<Product> productList= productDao.findProducts(query);
        for(Product product : productList){
            Product4Search product4Search=new Product4Search();
            product4Search.setId(product.getId());
            product4Search.setCity(product.getCity());
            product4Search.setTitle(product.getTitle());
            product4Search.setRentType(product.getRentType());
            product4Search.setPrice(product.getPrice());
            product4Search.setImg(product.getImg());
            String houseType="";
            houseType+=product.getInfo().get("level").toString();//楼层
            houseType+="|";
            houseType+=product.getInfo().get("type").toString();//房型
            houseType+="-";
            houseType+=product.getHouseType();
            product4Search.setHouseType(houseType);
            productDao4ES.indexDoc(product4Search);//房型
        }
    }
}
